import IDValidator from "id-validator";
import person from "@/validator/person";

let idValidator = new IDValidator();

let XMValidator = (rule, value, callback) => {

    if (value === undefined) {

        if (!rule.field || rule.field === '') {
            return callback(new Error('姓名不能为空'))
        }
    } else if (!value || value === '') {
        return callback(new Error('姓名不能为空'))
    }

    callback();
}

let ZJHMValidator = (rule, value, callback) => {

    if (!value || value === '') {
        return callback(new Error('身份证号码不能为空'))
    }
    if (!idValidator.isValid(value)) {
        return callback(new Error('身份证号码不正确'))
    }
    callback();
}

let CSRQValidator = (rule, value, callback) => {

    // if (value.year === "" || value.month === "" || value.day === "") {
    //     return callback(new Error('请填写出生日期'))
    // }
    if (!value || value === "") {
        return callback(new Error('请填写出生日期'))
    }
    callback();
}

let phoneValidator = (rule,value,callback)=>{

    if (!value || value === '') {
        return callback(new Error('手机号码不能为空'))
    }
    // if (person.phone(value)) {
    //     return callback(new Error('手机号码不正确'))
    // }
    callback();

}

export default {
    XM: [
        {required: true, validator: XMValidator, trigger: 'blur'}
    ],
    ZJHM: [
        {required: true, validator: ZJHMValidator, trigger: 'blur'}
    ],
    XB: {required: true, message: "性别不能为空", trigger: 'blur'},
    MZ: {required: true, message: "民族不能为空", trigger: 'blur'},
    DH: {required: true, validator: phoneValidator, trigger: 'blur'},
    DZ: {required: true, message: "地址不能为空", trigger: 'blur'},
    CSRQ: {required: true, validator: CSRQValidator, trigger: 'blur'},

    QSR:{required: true, message: "权属人不能为空", trigger: 'blur'},

    DHList: {
        required: true, validator: (rules, value, callback) => {

            if (value && value.length === 0) {
                return callback(new Error('电话不能为空'))
            }
            callback();
        }, trigger: 'focus'
    },

    DZList: {
        required: true, validator: (rules, value, callback) => {

            if (value && value.length === 0) {
                return callback(new Error('住址不能为空'))
            }
            callback()
        }, trigger: 'focus'
    }
}